//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tyr_fnccontainer\addHandler.sqf"
#include <core_macros.h>

TRACE_2("Start",_this select 0, count _this);

private ["_handler","_function","_idx","_array","_handlers_list","_functions_list"];
_handler = toupper(_this select 0);
_function = _this select 1;

if (TYR_FUNCTIONS_IS_BUSY) exitWith {
	_this spawn {
		TRACE("Wait");
		waituntil {!TYR_FUNCTIONS_IS_BUSY};
		_this call tyr_fnc_addHandler;
	};
};
TYR_FUNCTIONS_IS_BUSY = true;
if (isnil "TYR_FUNCTIONS_HANDLER_LIST") then {TYR_FUNCTIONS_HANDLER_LIST = [[],[]];};

_handlers_list = TYR_FUNCTIONS_HANDLER_LIST select 0;
_functions_list = TYR_FUNCTIONS_HANDLER_LIST select 1;

_idx = _handlers_list find _handler;
if (_idx < 0) then {
	_idx = count _handlers_list;
};
_handlers_list set [_idx, _handler];
_array = [];
if (_idx < count _functions_list) then {
	_array = _functions_list select _idx;
};
_array set [count _array, _function];
_functions_list set [_idx,_array];
TYR_FUNCTIONS_HANDLER_LIST = [_handlers_list,_functions_list];
TYR_FUNCTIONS_IS_BUSY = false;
TRACE_1("End",count _array);
count _array;